package com.mj.mafeed.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.mj.mafeed.R;
import com.mj.mafeed.adapter.ProductAdapter;
import com.mj.mafeed.bean.ProductJSON;
import com.mj.mafeed.bean.ProductListJSON;
import com.mj.mafeed.service.ProductService;
import com.mj.mafeed.swipeback.SwipeBackActivity;
import com.mj.mafeed.util.DateUtils;
import com.mj.mafeed.util.NetUtils;
import com.mj.mafeed.widget.LoadingDialog;
import com.mj.mafeed.widget.XListView;
import com.mj.mafeed.widget.XListView.IXListViewListener;

/**
 * 产品列表
 * @author zhaominglei
 * @date 2014-12-6
 * 
 */
public class ProductListActivity extends SwipeBackActivity implements OnClickListener,IXListViewListener {
	@SuppressWarnings("unused")
	private static final String TAG = ProductListActivity.class.getSimpleName();
	private ProductService productService = new ProductService();
	private ImageView goHome;
	private LoadingDialog loadingDialog; //加载框
	private XListView productListView;
	private List<ProductJSON> products = new ArrayList<ProductJSON>();
	private ProductAdapter productAdapter;
	private int page = 1; //页码
	@SuppressWarnings("unused")
	private String referer; //来自那个链接
	private String keyword; //关键词
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_product_list);
		
		init();
	}
	
	private void init() {
		goHome = (ImageView) findViewById(R.id.product_list_gohome);
		productListView = (XListView) findViewById(R.id.product_listview);
		productAdapter = new ProductAdapter(this, products);
		productListView.setPullLoadEnable(true);
		productListView.setXListViewListener(this);
		productListView.setAdapter(productAdapter);
		
		goHome.setOnClickListener(this);
		loadingDialog = new LoadingDialog(this);
		productListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				TextView productId = (TextView)view.findViewById(R.id.product_item_template_id);
				Intent intent = new Intent(view.getContext(), ProductDetailActivity.class);
				intent.putExtra("productId", Long.valueOf(productId.getText().toString()));
				intent.putExtra("keyword", keyword);
				intent.putExtra("referer", "ProductListActivity");
				startActivity(intent);
			}
		});
		
		referer = getIntent().getStringExtra("referer");
		keyword = getIntent().getStringExtra("keyword");
		getProducts();
	}
	
	private void getProducts() {
		if (NetUtils.isConnected(getApplicationContext())) {
			loadingDialog.show();
			new ProductListAsyncTask().execute("");
		} else {
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
			onLoad();
		}
	}

	@Override
	public void onRefresh() {
		getProducts();
	}

	@Override
	public void onLoadMore() {
		getProducts();
		page++;
	}
	
	//刷新结束
	public void onLoad() {
		productListView.stopRefresh();
		productListView.stopLoadMore();
		productListView.setRefreshTime(DateUtils.format(new Date(), DateUtils.datePattern));
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.product_list_gohome:
			ProductListActivity.this.finish();
			break;

		default:
			break;
		}
	}
	
	@Override
	public void onBackPressed() {
		ProductListActivity.this.finish();
	}

	public class ProductListAsyncTask extends AsyncTask<String, String, ProductListJSON> {
		@Override
		protected ProductListJSON doInBackground(String... params) {
			return productService.getProduct(getApplicationContext(), keyword, page);
		}

		@Override
		protected void onPostExecute(ProductListJSON result) {
			loadingDialog.dismiss();
			if (result != null && result.getRecordList() != null && result.getRecordList().size() > 0) {
				productAdapter.setProducts(result.getRecordList());
				productAdapter.notifyDataSetChanged();
				onLoad();
			}
			if (result != null && !result.isHasNextPage()) {
				Toast.makeText(getApplicationContext(), R.string.load_more_finish,	Toast.LENGTH_SHORT).show();
				productListView.setPullLoadEnable(false);
			} 
		}
	}
}
